package com.songlanyun.modules.shop.controller.admin;

import java.util.Arrays;
import java.util.Map;

import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.Constant;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.applyareaagent.service.ApplyAreaAgentService;
import com.songlanyun.modules.exception.ShopException;
import com.songlanyun.modules.shop.model.dto.ShopDTO;
import com.songlanyun.modules.shop.model.dto.ShopUpdateRecordDTO;
import com.songlanyun.modules.user.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.shop.entity.ShopUpdateRecordEntity;
import com.songlanyun.modules.shop.service.ShopUpdateRecordService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;

import javax.annotation.Resource;


/**
 * 商家修改商家信息申请记录表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-02-02 14:05:41
 */
@RestController
@RequestMapping("admin/shop/update/record")
public class ShopUpdateRecordController extends AbstractController {
    @Autowired
    private ShopUpdateRecordService shopUpdateRecordService;

    @Resource
    private ApplyAreaAgentService applyAreaAgentService;

    /**
     * 商家修改信息列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //平台查询全部的
        setShopId(params);
        //设置区县代理参数
        if (getAreaAgentId() != 0L) {
            applyAreaAgentService.setAreaAgentParams(getAreaAgentId(), params);
        }
        PageUtils page = shopUpdateRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        ShopUpdateRecordEntity shopUpdateRecord = shopUpdateRecordService.getById(id);

        return R.ok().put("data", shopUpdateRecord);
    }

    /**
     * 商家修改商家信息申请
     */
    @SysLog("商家修改商家信息申请")
    @RepeatSubmit("3")
    @PostMapping("/apply")
    public R save(@RequestBody ShopUpdateRecordDTO dto) {
        ValidatorUtils.validateEntity(dto);
        shopUpdateRecordService.saveRecord(dto, getShopId());

        return R.ok();
    }

    /**
     * 平台查看待审核列表
     */
    @GetMapping("/audit-list")
    public R auditList(@RequestParam Map<String, Object> params) {
        //平台查询全部的
        setShopId(params);
        params.put("audit_status", Constant.AUDITWAIT);//查询待审核列表
        PageUtils page = shopUpdateRecordService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 平台审核商家修改申请
     */
    @SysLog("平台审核商家修改申请")
    @RepeatSubmit("3")
    @PutMapping("/audit")
    public R update(@RequestBody ShopUpdateRecordEntity record) {
        verifyAdmin();//平台才能操作
        if (record.getId() == null || record.getAuditStatus() == null) {
            throw new RRException(ShopException.MUST_PARAMETER_NOT_NULL);
        }
        record.setAuditMan(getUserId());//审核人
        shopUpdateRecordService.updateRecord(record);

        return R.ok();
    }


    /**
     * 删除
     */
    @SysLog("删除商家修改申请")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        ShopUpdateRecordEntity record = shopUpdateRecordService.getById(id, true);
        if (record.getAuditStatus() != 0) {
            throw new RRException(ShopException.SHOP_UPDATE_COMPLETE_NOT_DEL);
        }
        shopUpdateRecordService.removeById(id);

        return R.ok();
    }

}
